<?php
class pdfCartList extends TCPDF
{
    protected static $m_logoW = 566;
    protected static $m_logoH = 86;
    protected static $m_a4width = 595;
    protected static $m_a4height = 841;
    
    protected static $m_tinySize = 10;
    
    protected static $m_smallSize = 12;
    
    protected static $m_mediumSize = 16;
    
    protected static $m_bigSize = 20;
    
//    protected static $m_fontFamily = 'helvetica';
    protected static $m_fontFamily = 'freeserif';
    
    protected static $m_lineStyle = array(
        'width' => 0.1
        , 'cap' => 'butt'
        , 'join' => 'miter'
        , 'dash' => 0
        , 'color' => array(0x00, 0x33, 0x66)
    );
    
    // Quick and dirty solution :)
    const WEB_DIR               = '';
    
    protected static $m_I18N = null;

    public function Header()
    {
        // Logo
        $this->Image(self::WEB_DIR . 'images/top_pdfui.png', 0, 17, self::$m_logoW, self::$m_logoH, '', '', 'T', '', 1, 'C');
    }


    /**
     * Prepares multi page PDF, containing short information about passed adverts
     *
     * @param Receipt $obj 
     */
    public function prepare($obj, $customer)
    {
        /* @var $obj Receipt */
        /* @var $item ReceiptItem */

        // Load I18N to use later
        self::$m_I18N = sfContext::getInstance()->getI18N();
        
        // set some language dependent data:
        $lg = Array();
        $lg['a_meta_charset'] = 'UTF-8';
        $lg['a_meta_language'] = 'lt';
        $lg['w_page'] = 'page';

        //set some language-dependent strings
        $this->setLanguageArray($lg);

        sfLoader::loadHelpers('Number');
        sfLoader::loadHelpers('Date');

        $advertSpacing = 12;
        $imageX = 40;
        $textX = 27;
        $startY = self::$m_logoH + 20;
        
        // Reduce A4 width by image size and margin
        $textW = self::$m_a4width - $textX;
        
        // Horizontal line after advert
        $lineW = self::$m_a4width - 2 * $imageX;
        
        $onPage = 1;
        $advertY = $startY;
        $this->AddPage();
        
        // Get customer info
        /* @var $customer Customer */
        $user = User::getOrCreate($obj->getUserId());
        
        // TODO: if $customer not egsists
        //sfActions::forward404Unless($customer);
        
        $this->setXY($textX, $advertY);
        $this->SetFillColor(255, 255, 255);
        $this->SetTextColor(0x00, 0x33, 0x66);
        $this->SetFont(self::$m_fontFamily, 'B', self::$m_tinySize);
        $advertY += $advertSpacing;   
        $this->setXY($textX, $advertY);     
        //$this->Cell(595, 7, date("Y-m-d"), 0, 0, 'C', 1);
        $this->Cell(595, 7, $customer->getCreatedAt("Y-m-d"), 0, 0, 'C', 1);
        $advertY += 4;
        $advertY += $advertSpacing;
        $this->setXY($textX, $advertY);
        $this->Line($textX, $advertY, $textW-2, $advertY, self::$m_lineStyle);
        $advertY += $advertSpacing;
        $this->setXY($textX+5, $advertY);
        $this->Cell(595, 7, $customer->getName(), 0, 0, 'L', 1);
        $advertY += $advertSpacing;
        
        $this->setXY($textX+5, $advertY);
        $this->SetFont(self::$m_fontFamily);
        $this->Cell(595, 7, $customer->getSurname(), 0, 0, 'L', 1);
        $advertY += $advertSpacing;
        
        $this->setXY($textX+5, $advertY);
        $this->Cell(595, 7, $customer->getStreet(), 0, 0, 'L', 1);
        $advertY += $advertSpacing;
        
        $this->setXY($textX+5, $advertY);
        $this->Cell(595, 7, $customer->getPhoneNr(), 0, 0, 'L', 1);
        $advertY += $advertSpacing;
        if( $customer->getIsCompany() == 1 )
        {
            $this->setXY($textX+5, $advertY);
            $this->Cell(595, 7, $customer->getCompanyName(), 0, 0, 'L', 1);
            $advertY += $advertSpacing;
            
            $this->setXY($textX+5, $advertY);
            $this->Cell(595, 7, $customer->getCompanyNr(), 0, 0, 'L', 1);
            $advertY += $advertSpacing;
            
            $this->setXY($textX+5, $advertY);
            $this->Cell(595, 7, $customer->getFiscalCode(), 0, 0, 'L', 1);
            $advertY += $advertSpacing;
            $onPage +=3;
        }
        
        $this->Cell(595, 7, $user->getEmail(), 0, 0, 'L', 1);
        //$advertY += $advertSpacing;
        $this->setXY($textX, $advertY);
        $some = $this->MultiCell(535, $advertSpacing, /*$obj->getRemarks()*/'' , 0, 'L', false, 1, $textX+6, $advertY, true, 0, false, true, 0, 'T', false);   
        $advertY += $advertSpacing*($some+1);
        $this->setXY($textX, $advertY);
        //$this->Cell(595, 7, $obj->getRemarks(), 0, 0, 'L', 1);
        //$advertY += $advertSpacing;
        //$this->setXY($textX, $advertY);
        $this->Line($textX, $advertY, $textW-2, $advertY, self::$m_lineStyle);
        $advertY += $advertSpacing;
        $this->SetDrawColor(200, 200, 200);
        
        $c = new Criteria();
        $c->add(ReceiptItemPeer::RECEIPT_ID, $obj->getId());
        $arReceiptItems = ReceiptItemPeer::doSelect($c);
        
        $this->setXY($textX, $advertY);
        $this->SetFont(self::$m_fontFamily, 'B');
        // Header
        $w = array(220, 30, 90, 75, 50, 75);
        
        $this->Cell($w[0], 7, self::$m_I18N->__('Receipt name'), 1, 0, 'L', 1);
        $this->Cell($w[1], 7, ucfirst(self::$m_I18N->__('A/I')), 1, 0, 'C', 1);
        $this->Cell($w[2], 7, ucfirst(self::$m_I18N->__('code')), 1, 0, 'C', 1);
        $this->Cell($w[3], 7, ucfirst(self::$m_I18N->__('price')), 1, 0, 'C', 1);
        $this->Cell($w[4], 7, ucfirst(self::$m_I18N->__('quantity')), 1, 0, 'C', 1);
        $this->Cell($w[5], 7, ucfirst(self::$m_I18N->__('sum')), 1, 0, 'C', 1);
        $advertY += $advertSpacing;
        $onPage +=9+($some+1);
        for($i = 0; $i<100; $i++)
        {
        foreach( $arReceiptItems as $item )
        {
            
            // Check if advert will fit on current page
            if( !$this->advertWillFit($onPage, $advertSpacing) )
            {
                log_this('Adding new PDF page to fit advert (' . $onPage . ' on page)');
                $this->AddPage();
                $onPage = 1;
                $advertY = $startY;
                $advertY += $advertSpacing;
                $this->setXY($textX, $advertY);
                $this->SetFont(self::$m_fontFamily, 'B');
                // Header
                $w = array(210, 30, 90, 75, 50, 75);
                
                $this->Cell($w[0], 7, self::$m_I18N->__('Receipt name'), 1, 0, 'L', 1);
		        $this->Cell($w[1], 7, ucfirst(self::$m_I18N->__('A/I')), 1, 0, 'C', 1);
		        $this->Cell($w[2], 7, ucfirst(self::$m_I18N->__('code')), 1, 0, 'C', 1);
		        $this->Cell($w[3], 7, ucfirst(self::$m_I18N->__('price')), 1, 0, 'C', 1);
		        $this->Cell($w[4], 7, ucfirst(self::$m_I18N->__('quantity')), 1, 0, 'C', 1);
		        $this->Cell($w[5], 7, ucfirst(self::$m_I18N->__('sum')), 1, 0, 'C', 1);
                $advertY += $advertSpacing;
            }}

            $this->setXY($textX, $advertY);
            $this->SetFont(self::$m_fontFamily);
            // Data
            $fill = 0;
            $special = '';
//            if($item->hasTag(Product::OUTLET_TAG))
//            {
//            	$special = 'I';
//            }
//            if($item->hasTag(Product::PROMO_TAG))
//            {
//                $special = 'A';
//	            if($item->hasTag(Product::OUTLET_TAG))
//	            {
//	                $special .= '/I';
//	            }
//            }
            $this->Cell($w[0], 6, $item->getName(), 'LRB', 0, 'L', $fill, '', 1);
            $this->Cell($w[1], 6, $special, 'LRB', 0, 'C', $fill, '', 1);
            $this->Cell($w[2], 6, ProductPeer::retrieveByPk($item->getObjId())->getCode(), 'LRB', 0, 'L', $fill, '', 1);
            $this->Cell($w[3], 6, format_currency($item->getPriceOfItem(), $item->getCurrencyCode()), 'LRB', 0, 'C', $fill);
            $this->Cell($w[4], 6, $item->getQuantity(), 'LRB', 0, 'C', $fill);
            $this->Cell($w[5], 6, format_currency($item->getPriceOfItem() * $item->getQuantity(), $item->getCurrencyCode()), 'LRB', 0, 'C', $fill);
            $advertY += $advertSpacing;
            log_this("Cell [$onPage] at: ($textX, $advertY)");

            // Next advert will be below
            $onPage++;
            log_this('----------------------------------------------------------------');
        }  // END FOREACH ( loop through objects )
        
    
        // Check if advert will fit on current page
        $totalsH = 14;
        if( !$this->advertWillFit($onPage, $totalsH) )
        {
            log_this('Adding new PDF page to fit advert (' . $onPage . ' on page)');
            $this->AddPage('P', 'A4');
            $advertY = $startY;
        }
        $advertY += $advertSpacing;
        $this->setXY($textX + 340, $advertY);
        $this->SetFont(self::$m_fontFamily, 'B');

        // TODO: I18N!!!
        $fill = 0;
        $tax = $obj->getTotalPrice() / (($obj->getTax()/100)+1);
        $w = array(340, 75, 50, 75);
        $this->Cell($w[1], 7, ucfirst(self::$m_I18N->__('total')), 1, 0, 'L', 1);
        $this->SetFont(self::$m_fontFamily);
        $this->Cell($w[2], 7, $obj->getItemsQnt(), 1, 0, 'C', 1);
        $this->Cell($w[3], 7, format_currency($obj->getTotalPrice() - $obj->getTax(), $obj->getCurrencyCode()), 1, 0, 'C', 1);
        $advertY += $advertSpacing;
        $this->SetFont(self::$m_fontFamily, 'B');
        $this->setXY($textX + 340, $advertY);
        $this->Cell($w[1], 7, ucfirst(self::$m_I18N->__('VAT (%1%%)', array('%1%' => sfConfig::get('app_cart_vat')))), 1, 0, 'L', 1);
        $this->SetFont(self::$m_fontFamily);
        $this->Cell($w[2], 7, '', 1, 0, 'L', 1);
        $this->Cell($w[3], 7, format_currency($obj->getTax(), $obj->getCurrencyCode()), 1, 0, 'C', 1);
        $advertY += $advertSpacing;
        $this->SetFont(self::$m_fontFamily, 'B');
        $this->setXY($textX, $advertY);
        $this->Cell($w[0], 7, ucfirst(self::$m_I18N->__('Sum in words')), 0, 0, 'L', $fill);
        $this->Cell($w[1], 7, '', 1, 0, 'C', 1);
        $this->Cell($w[2], 7, '', 1, 0, 'C', 1);
        $this->Cell($w[3], 7, '', 1, 0, 'C', 1);
        $advertY += $advertSpacing;
        $this->setXY($textX, $advertY);
        $this->SetFont(self::$m_fontFamily);
        $ct = ($obj->getTotalPrice() * 100)%100;
        $this->Cell($w[0], 7, ucfirst(/*MyHelper::PriceToWord($obj->getTotalPrice()).*/ ' Lt '. $ct . ' ct') , 0, 0, 'L', $fill);
        $this->SetFont(self::$m_fontFamily, 'B');
        $this->Cell($w[1], 7, ucfirst(self::$m_I18N->__('total')) . ':', 1, 0, 'L', 1);
        $this->SetFont(self::$m_fontFamily);
        $this->Cell($w[2], 7, '', 1, 0, 'C', 1);
        $this->Cell($w[3], 7, format_currency($obj->getTotalPrice(), $obj->getCurrencyCode()), 1, 0, 'C', 1);
        $advertY += $advertSpacing;
        
        $filename = sprintf(
            'hipika_cart_%s.pdf'
            , date('Ymd')
        );

        $this->Output(urlencode($filename), 'D');
        
    }
    
    /**
     * Returns whether advert will fit on current A4 PDF page
     *
     * @param integer $onPage number on page (starts from 1)
     * @param integer $imageH height of image to check
     * @return boolean whether advert fits
     */
    protected function advertWillFit($onPage, $imageH)
    {
        // Where upper part of advert will be
        $advertY = $onPage * $imageH + self::$m_logoH + 10;
        log_this("Check if upper advert Y [$advertY] fits to A4 height (w/o footer) " . (self::$m_a4height - 50));
        return $advertY < self::$m_a4height - 85;
    }
    

    // Page footer
    public function Footer()
    {
        $w = self::$m_logoW;
        $h = 24;
        $y = self::$m_a4height - $h - 6;
        // Logo
        $this->Image(self::WEB_DIR . 'images/bottom_pdfui.png', 0, $y, $w, $h, '', '', 'B', '', 1, 'C');
        // Position from bottom
        $this->SetXY(0, -30);
        // Set font
        $this->SetFont(self::$m_fontFamily, '', self::$m_tinySize);
        //$this->SetFillColor(64, 103, 162);
        // Page number
        $this->Cell(self::$m_a4width, 25, 'Page ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages(), 0, 0, 'C', 0);
    }
}
?>
